package detector.home;

import java.util.HashSet;
import java.util.Set;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnKeyListener;
import android.view.ViewGroup;
import android.view.inputmethod.EditorInfo;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.TextView.OnEditorActionListener;
import android.widget.Toast;
import detector.audio.ServerComm;
import detector.audio.processing.AudioClip;
import detector.audio.processing.Monitor;
import detector.audio.processing.PeakHandler;
import detector.audio.processing.SoundClass;

/**
 * This activity should be run if it is the user's first time. Call it if that
 * is the case. It walks through the steps for training. You can also call this
 * to add a sound, it won't prompt for the location if that is the case.
 * 
 * @author Kyle
 * 
 */
public class FirstTimeActivity extends AbstractTrainActivity {

	private FirstTimeActivity thisActivity = this;
	private static String DATA_FILE_LOCATION = "datafile.dat";

	// The layouts that contain each section
	ViewGroup lytLocation, lytTrain, lytNameSound;

	private static final String TAG = "TrainActivity";
	protected static final int EXAMPLES_REQUIRED = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.bigmain);
		initializeTrainingViews();


		// Start with the training-related views grayed out and
		// inactive and the sound naming button disabled
		setTrainSectionActive(false);

		// Set up the top EditText to take the location name
		// from the user and enable the training section when
		// done
		// Get the EditText where the user enters their location
		final EditText txtLocation = (EditText) findViewById(R.id.txtLocation);
		// When the user presses enter,
		// hide the keyboard, hide the flashing cursor
		// in txtLocation
		txtLocation.setOnEditorActionListener(new OnEditorActionListener() {
			public boolean onEditorAction(TextView tv, int action, KeyEvent arg2) {
				InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
				imm.hideSoftInputFromWindow(txtLocation.getWindowToken(), 0);
				//Only set location name when enter is pressed on this
				ServerComm.setLocationName(tv.getText().toString());
				// stop the flashing cursor from showing
				// up in txtLocation any more
				tv.setCursorVisible(false);
				
				return true;
			}
		});
		// Make txtLocation only show the flashing text entry cursor
		// when it is clicked on
		txtLocation.setOnClickListener(new OnClickListener() {
			public void onClick(View v) {
				// Show cursor only when clicked
				((EditText) v).setCursorVisible(true);
			}
		});
		// Make it so the training section is enabled as soon
		// as the user enters text
		txtLocation.addTextChangedListener(new TextWatcher() {
			public void afterTextChanged(Editable arg0) {
			
				
			}
			
			public void beforeTextChanged(CharSequence arg0, int arg1,
					int arg2, int arg3) {
				
			}
			
			public void onTextChanged(CharSequence arg0, int arg1, int arg2,
					int arg3) {
				if (!isTrainSectionActive()) {
					setTrainSectionActive(true);
				}				
			}	
		});
	}

	@Override
	public void finish() {
		Monitor.getMonitor().stopListening();
		super.finish();
	}
	
	@Override
	//Tell caller that we didn't finish this activity all the way through
	public void onBackPressed() {
	    this.setResult(RESULT_CANCELED);
	    finish();
	}

}